package com.szms.mobile.listeners;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.szms.mobile.BaseFragment;
import com.szms.mobile.MainActivity;
import com.szms.mobile.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * 添加 fragment事件
 */
public class AddFragmentListener implements View.OnClickListener{

    private BaseFragment fragment;
    Map<Integer,List<Class>> menuMap;

    private Consumer<BaseFragment> fragmentConsumer;

    MainActivity activity;

    public AddFragmentListener(BaseFragment _fragment, Map<Integer, List<Class>> _menuMap, MainActivity _activity){
        this.fragment = _fragment;
        menuMap = _menuMap;
        activity = _activity;
    }

    public AddFragmentListener(BaseFragment _fragment, Map<Integer, List<Class>> _menuMap, MainActivity _activity, Consumer<BaseFragment> _consumer){
        this(_fragment,_menuMap,_activity);
        fragmentConsumer = _consumer;
    }

    @Override
    public void onClick(View v) {
        this.AddFragment(fragment);
        this.setActivatedMenu(fragment);
        if (this.fragmentConsumer!=null){
            fragmentConsumer.accept(fragment);
        }
    }

    /**
     * 添加
     * @param fragment
     */
    private void AddFragment(BaseFragment fragment){
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        if(this.isReAdd(fragmentManager,fragment)){
            return;
        }

        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frameMain,fragment);
        if(fragment.getTopFragment()){ //如果是顶层的那么就清空所有栈
            fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }else{
            transaction.addToBackStack(null);//添加到管理栈
        }
        transaction.commit();

        setActivatedMenu(fragment);
    }

    // 在Activity中
    public List<Fragment> getCurrentFragments(FragmentManager fragmentManager) {
        List<Fragment> fragments = fragmentManager.getFragments();
        List<Fragment> visibleFragments = new ArrayList<>();
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible()) {
                visibleFragments.add(fragment);
            }
        }
        return visibleFragments; // 当前没有可见的Fragment
    }

    public Boolean isReAdd(FragmentManager fragmentManager,Fragment fragment){
        List<Fragment> currentFragments = this.getCurrentFragments(fragmentManager);
        boolean b = currentFragments.stream().anyMatch(f -> f.getClass().equals(fragment.getClass()));
        return b;
    }

    /**
     * 设置当前菜单
     * @param fragment
     */
    public void setActivatedMenu(Fragment fragment){
        for (Integer menuId : menuMap.keySet()) {
            List<Class> fragments = menuMap.get(menuId);

            LinearLayout layout = activity.findViewById(menuId);
            TextView textView = (TextView)layout.getChildAt(1);

            if(fragments.contains(fragment.getClass())){
                textView.setTextColor(activity.getResources().getColor(R.color.aqua,null));
            }else{
                textView.setTextColor(activity.getResources().getColor(R.color.white,null));
            }
        }
    }
}
